package com.fruits.manage.config;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.wall.WallFilter;
import com.fruits.manage.web.controller.IndexController;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.Dialect;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;

/**
 * 应用配置
 * 
 * @author chexl
 * 
 * @date 2015-1-16
 * @time 下午05:25:08
 */
public class AppConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		loadPropertyFile("jdbc.properties");
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);
	}

	@Override
	public void configPlugin(Plugins me) {
		String jdbcUrl = getProperty("jdbc.url");
		String jdbcUsername = getProperty("jdbc.username");
		String jdbcPassword = getProperty("jdbc.password");

		boolean debug = Boolean.valueOf(getProperty("jdbc.debug", "false"));

		StatFilter statFilter = new StatFilter();

		WallFilter wallFilter = new WallFilter();
		wallFilter.setDbType(JdbcConstants.MYSQL);

		String validationQuery = "SELECT 1 FROM DUAL";

		DruidPlugin dp = new DruidPlugin(jdbcUrl, jdbcUsername, jdbcPassword, JdbcConstants.MYSQL_DRIVER);
		dp.addFilter(statFilter);
		dp.addFilter(wallFilter);
		dp.setValidationQuery(validationQuery);

		me.add(dp);

		Dialect dialect = new MysqlDialect();

		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		arp.setShowSql(debug);
		arp.setDevMode(debug);
		arp.setDialect(dialect);

		me.add(arp);
	}

	@Override
	public void configInterceptor(Interceptors me) {

	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("ctx_path"));
	}

}
